import allConfig from "../../config/index.js"

const request = (url = '' , date = {}, type = 'POST' , titles = '加载中') => {
	// https://www.cnblogs.com/ffyun/p/13919481.html
	// https://www.jianshu.com/p/bd4ce054eb7b
	// #ifndef H5
		url = allConfig.url + url
	// #endif
	const token = uni.getStorageSync('TOKEN');
	return new Promise((resolve, reject) => {
		// uni.showLoading({
		// 	title: titles
		// });
		uni.request({
			method: type,
			url: url,
			header:{'token' : token},
			data: date     
		}).then((response) => {
			uni.hideLoading();
			uni.stopPullDownRefresh();
			let [error, res] = response; 
			resolve(res);
			if(res.data.code==-300){
				uni.removeStorageSync('TOKEN')
				uni.showModal({
					title: '提示',
					content: '该账号在异地登陆！',
					success: function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
							uni.reLaunch({
								url:'/pages/login/login'
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
		}).catch(error => {
			uni.hideLoading();
			uni.stopPullDownRefresh();
			
			let [err, res] = error;
			reject(err)
		})
	});
}
export default request